package com.example.lenovo.a203_yhy_ordering_3.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.lenovo.a203_yhy_ordering_3.R;
import com.example.lenovo.a203_yhy_ordering_3.activity.FoodListActivity;
import com.example.lenovo.a203_yhy_ordering_3.adapter.FoodListAdapter;
import com.example.lenovo.a203_yhy_ordering_3.adapter.ShopAdapter;
import com.example.lenovo.a203_yhy_ordering_3.beans.ShopBean;
import com.example.lenovo.a203_yhy_ordering_3.listener.ShopListener;
import com.example.lenovo.a203_yhy_ordering_3.model.ShopModel;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

public class ShopFragment extends BaseFragment {
    private List<ShopBean> list;
    private RecyclerView recyclerView;
    private ShopAdapter shopAdapter;

    public ShopFragment() {
    }

    private ShopListener shopListener = new ShopListener(){
        @Override
        public void onResponse(List<ShopBean> shopBean) {
            list=shopBean;
            shopAdapter.setList(list);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getContext(), "失败："+msg, Toast.LENGTH_SHORT).show();

        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.activity_shop_fragment,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ShopModel shopModel = new ShopModel();
        shopModel.getShopResult(shopListener);

        shopAdapter = new ShopAdapter();
        recyclerView = view.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));

        ShopAdapter.OnItemClickListener onItemClickListener= new ShopAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                Intent intent=new Intent(getContext(),FoodListActivity.class);
                intent.putExtra("shop_id",list.get(position).getShop_id());
                intent.putExtra("shopname",list.get(position).getShopname());
                startActivity(intent);

            }
        };

        shopAdapter.setOnItemClickListener(onItemClickListener);
        recyclerView.setAdapter(shopAdapter);
    }
}
